package ch.ti.bfh.app.loodel.web.controller;

import ch.ti.bfh.app.loodel.service.admin.LearningEventAdminService;
import ch.ti.bfh.app.loodel.service.admin.LearningEventAdministrationException;
import ch.ti.bfh.app.loodel.service.vote.AllMembersTimeSlotVotesData;
import ch.ti.bfh.app.loodel.service.vote.LearningEventVoteService;
import ch.ti.bfh.app.loodel.web.model.LearningEventAdminModel;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.omnifaces.util.Messages;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import javax.faces.context.FacesContext;
import javax.inject.Inject;
import java.util.ArrayList;
import java.util.List;

@Component
@Scope("request")
public class LearningEventAdminController {

    private final LearningEventAdminService learningEventAdminService;

    private final LearningEventVoteService learningEventVoteService;

    private final LearningEventAdminModel learningEventAdminModel;

    @Inject
    public LearningEventAdminController(LearningEventAdminService learningEventAdminService, LearningEventVoteService learningEventVoteService, LearningEventAdminModel learningEventAdminModel) {
        this.learningEventAdminService = learningEventAdminService;
        this.learningEventVoteService = learningEventVoteService;
        this.learningEventAdminModel = learningEventAdminModel;
    }

    public void init() {
        if (FacesContext.getCurrentInstance().isPostback() == false) {
            initLearningEventAdminData();
        }
    }

    public void closeLearningEvent() throws LearningEventAdministrationException {
        List<Long> acceptedTimeSlots = new ArrayList<Long>();
        for (AllMembersTimeSlotVotesData timeSlotData : learningEventAdminModel.getAcceptedTimeSlots().keySet()) {
            if (learningEventAdminModel.getAcceptedTimeSlots().get(timeSlotData)) {
                acceptedTimeSlots.add(timeSlotData.getTimeSlotData().getTimeSlotId());
            }
        }
        if (CollectionUtils.isEmpty(acceptedTimeSlots)) {
            Messages.addGlobalWarn("You need to select at least one time slot");
        } else {
            learningEventAdminService.closeLearningEventVotes(learningEventAdminModel.getLearningEventId(), acceptedTimeSlots);
            Messages.addGlobalInfo("Learning Event Closed");
        }
    }

    public void acceptTimeSlotAction() {
        final AllMembersTimeSlotVotesData selectedTimeSlotData = learningEventAdminModel.getSelectedTimeSlotData();
        learningEventAdminModel.getAcceptedTimeSlots().put(selectedTimeSlotData, true);
    }

    public void rejectTimeSlotAction() {
        final AllMembersTimeSlotVotesData selectedTimeSlotData = learningEventAdminModel.getSelectedTimeSlotData();
        learningEventAdminModel.getAcceptedTimeSlots().put(selectedTimeSlotData, false);
    }

    public void enterLearningEventIdAction() {
        initLearningEventAdminData();
    }

    private void initLearningEventAdminData() {
        final String learningEventId = this.learningEventAdminModel.getLearningEventId();
        if (StringUtils.isNotEmpty(learningEventId)) {
            learningEventAdminModel.setLearningEventVoteData(learningEventVoteService.getLearningEventVoteData(learningEventId));
            initAllMemberTimeSlotVotes(learningEventId);
        }
    }


    private void initAllMemberTimeSlotVotes(String learningEventId) {
        final List<AllMembersTimeSlotVotesData> allMembersTimeSlotVotes = learningEventVoteService.getAllMembersTimeSlotVotes(learningEventId);
        learningEventAdminModel.setAllMembersTimeSlotVotesDatas(allMembersTimeSlotVotes);
        for (AllMembersTimeSlotVotesData allMembersTimeSlotVotesData : allMembersTimeSlotVotes) {
            learningEventAdminModel.getAcceptedTimeSlots().put(allMembersTimeSlotVotesData, allMembersTimeSlotVotesData.isTimeSlotAccepted());
        }
    }
}
